/*
 *  ExTexture2D.h
 *  opengl_test1
 *
 *  Created by jobs mac on 08-11-16.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */
#ifndef _EXTEXTURE2D_H_
#define _EXTEXTURE2D_H_
#include "graphic_public.h"
#include "CCGL.h"
#include "affine.h"
#include "CCTexture2D.h"
#include "CCTextureCache.h"


typedef cocos2d::CCTexture2D Texture2D;

class ExTexture2D{
	public:
	
	ExTexture2D(const char* filename) : m_tex(){
		m_tex = cocos2d::CCTextureCache::sharedTextureCache()->addImage(filename);
		ASSERT(NULL != m_tex);
	}
	ExTexture2D(Texture2D *tex) : m_tex(tex) {}	
	
	~ExTexture2D() {
		if (m_tex) m_tex->release();
	}
	//
	virtual void Draw() const;

	//按设置的透明度绘制
	void Draw(float alpha);
	//move without drawing
	void MoveToRect(cocos2d::CCRect rect);
	
	void DrawAtPoint(float x, float y){
		MovetoPoint(x, y);
		Draw();
	}
	void DrawInRect(cocos2d::CCRect rect)
	{
		MoveToRect(rect);
		Draw();
	}
	void ExDrawInRect(cocos2d::CCRect rect)
	{
		//MovetoPoint(rect.origin.x, rect.origin.y);		
		MovetoPoint(-m_tex->getContentSizeInPixels().width/2, -m_tex->getContentSizeInPixels().height/2);
		affine2d::scale(m_affine, rect.size.width/m_tex->getContentSizeInPixels().width, rect.size.height/m_tex->getContentSizeInPixels().height);
		Translate(rect.size.width/2, rect.size.height/2);
		Translate(rect.origin.x, rect.origin.y);
		Draw();		
	}
	
	void DrawWithTexRotate90() const;
	
	void Rotate90AndDrawInRect(cocos2d::CCRect rect)
	{
		MovetoPoint(-m_tex->getContentSizeInPixels().width/2, -m_tex->getContentSizeInPixels().height/2);
		affine2d::scale(m_affine, rect.size.width/m_tex->getContentSizeInPixels().width, rect.size.height/m_tex->getContentSizeInPixels().height);
		Translate(rect.size.width/2, rect.size.height/2);
		Translate(rect.origin.x, rect.origin.y);
		DrawWithTexRotate90();	
	}
	
	void Scale (float scale){
		affine2d::scale(m_affine, scale, scale);
	}
	
	void rotate(float angle){
		affine::affine_point2d_t center;
		center = m_tex->getContentSizeInPixels().width/2, m_tex->getContentSizeInPixels().height/2, 1;
		center = m_affine * center;		
		Translate(-center[0], -center[1]);
		affine2d::rotate(m_affine, angle);		
		Translate(center[0], center[1]);
	}
	
	void ExRotate(float angle) {
		affine::affine_point2d_t center;
		center = 0, 0, 1;
		center = m_affine * center;		
		Translate(-center[0], -center[1]);
		affine2d::rotate(m_affine, angle);		
		Translate(center[0], center[1]);
	}
	
	void ExScale(float scale)
	{
		affine::affine_point2d_t center;
		center = m_tex->getContentSizeInPixels().width/2, m_tex->getContentSizeInPixels().height/2, 1;
		center = m_affine * center;		
		Translate(-center[0], -center[1]);
		Scale(scale);
		Translate(center[0], center[1]);
	}
	
	void MovetoPoint(float x, float y){
		Indentity();
		Translate(x, y);
	}
	void Translate(float x, float y)
	{
		affine2d::translate(m_affine, x, y);
	}
	
	void Indentity(){
		affine2d::identity(m_affine);
	}
	
	inline Texture2D *getTexture() {
		return m_tex;
	}
	inline Texture2D *GetTexture() {
		return getTexture();
	}
	inline float_t getWidth() {
		return getTexture()->getContentSizeInPixels().width;
	}
	inline float_t getHeight() {
		return getTexture()->getContentSizeInPixels().height;
	}
	inline GLuint getName() {
		return getTexture()->getName();
	}
	
//	CGAffineTransform GetMatrix() const {
//		return CGAffineTransformMake(m_affine(0, 0), m_affine(0, 1), m_affine(1, 0), m_affine(1, 1), m_affine(2, 0), m_affine(2, 1));
//	}

private:
	Texture2D *m_tex;
	affine::transform2d m_affine;
	
NONCOPYABLE_OBJCT(ExTexture2D)

};

class ExTexture2DLight : public ExTexture2D{
public:
	ExTexture2DLight(const char * filename) : ExTexture2D(filename){}
	virtual void Draw() const{
		glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
		ExTexture2D::Draw();
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);		
	}
};



#endif
